package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.service.INursingLevelService;
import com.zzyl.nursing.vo.NursingLevelVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
@Slf4j
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        int flag = nursingLevelMapper.insert(nursingLevel);
        try {
            redisTemplate.delete(CacheConstants.NURSING_LEVEL_ENABLE_ALL_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        int flag = nursingLevelMapper.updateById(nursingLevel);
        try {
            redisTemplate.delete(CacheConstants.NURSING_LEVEL_ENABLE_ALL_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        return nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        int flag = nursingLevelMapper.deleteById(id);
        try {
            redisTemplate.delete(CacheConstants.NURSING_LEVEL_ENABLE_ALL_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    /**
     * 查询启用的护理等级列表
     * @return 结果
     */
    @Override
    public List<NursingLevel> getEnableNursingLevel() {
        // 1.查询缓存
        List<NursingLevel> list = null;
        try {
            list = (List<NursingLevel>) redisTemplate.opsForValue().get(CacheConstants.NURSING_LEVEL_ENABLE_ALL_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 2.缓存中有，直接返回
        if (CollUtil.isNotEmpty(list)) {
            log.warn("redis ---- 从缓存中查询数据 ----");
            return list;
        }

        // 3.缓存中没有，再查询数据库，放入缓存
        log.warn("DB ---- 从数据库中查询数据 ----");
        list = lambdaQuery().eq(NursingLevel::getStatus, 1).list();
        try {
            redisTemplate.opsForValue().set(CacheConstants.NURSING_LEVEL_ENABLE_ALL_KEY, list, 60 * 60 * 24, TimeUnit.SECONDS);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
